﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExtAspNet;
using System.Data;

namespace HY.WEB.SA
{
    public partial class sch_add : PageBase
    {
        public string us, um, ubh,sid;
        string tab = "school";
        protected void Page_Load(object sender, EventArgs e)
        {

            us = Session["ID"].ToString();
            um = Session["user"].ToString();
            ubh = Session["bh"].ToString();
               sid = Session["sid"].ToString();

           

            if (!IsPostBack)
            {
                if (!String.IsNullOrEmpty(Request["stype"]))
                {
                     hidstype.Text = Request["sid"].Trim();                
                }
                fill();
                ExtAspNet.PageContext.RegisterStartupScript
                       (
                       "var d=Ext.getCmp('" + ddlschool.ClientID + "');" +
                       " var oldInput=\"\";" +
                       " d.listWidth=500; " +
                          " d.addListener(\"specialkey\",function(f,e){ " +  //keyup
                       " var newInput=document.getElementById(\"" + ddlschool.ClientID + "\").value;" +
                        " if (oldInput==newInput) {return;} " +
                       " oldInput=newInput;" +
                        " __doPostBack('loaddata',newInput);" +
                        "});"
                       );

            }
            else
            {

                if (Request["__EVENTTARGET"] == "loaddata")
                {

                    string sqltxt = "SELECT * FROM school where charindex('" +
                        Request["__EVENTARGUMENT"].ToString() + "',schname)>0 ";
                    txtschool.Text = Request["__EVENTARGUMENT"].ToString();

                    DataTable dt = DAL.DbHelperSQL.SqlDataAdapter(sqltxt);
                    ddlschool.Items.Clear();
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        ddlschool.Items.Add(new ExtAspNet.ListItem(dt.Rows[i]["schname"].ToString(), dt.Rows[i]["schname"].ToString()));
                    }


                    //下面这句话是有道理的，extjs默认效果在autocomplete之后，值为变成下拉框的第一项，有点破坏效果，这里纠正过来，可以输入多个关键字  
                    ExtAspNet.PageContext.RegisterStartupScript("document.getElementById(\"" + ddlschool.ClientID + "\").value='" +
                             Request["__EVENTARGUMENT"] + "';");

                }


            }
     
        }




        void fill()
        {

            //客户性质
          DataTable  e = BLL.Bllbase.get_table("[sch_type]");
            ddltype.Items.Clear();
            for (int i = 0; i < e.Rows.Count; i++)
            {
                ddltype.Items.Add(new ExtAspNet.ListItem(e.Rows[i][2].ToString(), e.Rows[i][2].ToString()));
            }
            ddltype.DataBind();
            ddltype.Items.Add(new ExtAspNet.ListItem("", ""));

            //客户性质
             e = BLL.Bllbase.get_table("[sch_xz]");
            ddlxz.Items.Clear();
            for (int i = 0; i < e.Rows.Count; i++)
            {
                ddlxz.Items.Add(new ExtAspNet.ListItem(e.Rows[i][2].ToString(), e.Rows[i][2].ToString()));
            }
            ddlxz.DataBind();
            ddlxz.Items.Add(new ExtAspNet.ListItem("", ""));
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
          Model.SC.schools jb=new Model.SC.schools();

          jb.schbh = BLL.BllAutoCode.GetMaxBH("school"); //岗位编号
          if (jb.schbh == "") return;


          jb.schname = txtschool.Text;
          if (txtschool.Text != "")
              if (BLL.Bllbase.check_exits("school", "schname='" + txtschool.Text.Trim() + "'"))
              {
                  Alert.Show("校园名已经存在，请检查信息是否重复。"); return;
              }

          jb.person = txtperson.Text;

          if (txttel.Text != "")
              if (BLL.Bllbase.check_exits("school", "stel='" + txttel.Text.Trim() + "'"))
              {
                  Alert.Show("校园电话已经存在，请检查信息是否重复。"); return;
              }
        


          jb.stel =txttel.Text; 
          jb.sfax= txtfax.Text;
          jb.mtel = txttelm.Text;
          jb.qq = txtqq.Text;
          jb.msn = txtmsn.Text;

          jb.semail = txtemail.Text; 
          jb.saddress= txtaddress.Text; 
          jb.spost =txtpost.Text;
          jb.sweb = txtwebsite.Text;

          if (string.IsNullOrEmpty(date1.Text.Trim()))
              date1.Text = "1900-01-01";

          jb.rdate=DateTime.Parse(date1.Text.Trim());

          jb.type = ddltype.SelectedValue;
          jb.xz = ddlxz.SelectedValue;
          jb.sales = us;
          jb.updtime = DateTime.Now;
          jb.pjob = txtjob.Text;
          jb.psex = txtsex.Text;
          jb.psize = txtsize.Text;


            jb.memo= HttpUtility.HtmlEncode(FCKeditor1.Value);

             BLL.SC.schools sco = new BLL.SC.schools();
             sco.Add(jb);


             ////写入收藏表
             Alert.Show(BLL.BllSales.intosales("学校", jb.schbh,jb.schname, ubh, um,"SO"));

             //更新业务员操作记录
             BLL.Bllbase.update_rec(tab, um, " schbh='" + jb.schbh + "' ");

            Alert.Show("添加完成 ");
            PageContext.RegisterStartupScript(ActiveWindow.GetWriteBackValueReference(hidstype.Text) + ActiveWindow.GetHidePostBackReference());
        }

       
    }
}